package org.textreport.lang;

import java.util.ArrayList;
import java.util.List;

import org.textreport.core.ExecutionContext;

public class JSExpression implements Expression {
	private List<Expression> body;
	private int sourceLine;
	private Expression parent;
	
	public JSExpression(Expression parent){
		body = new ArrayList<Expression>();
		this.parent = parent;
	}
	
	@Override
	public void eval(ExecutionContext ctx) {
		try{
			ctx.getOut().bookmark();
			
			for(Expression e: body){
				e.eval(ctx);
			}
			
			String bodyScript = ctx.getOut().restoreBookmark();

			ctx.evalJavaScript(bodyScript);
		}catch(Exception e ){
			ctx.error("JavaScript inline()", this, "Erro ao executar bloco JavaScript.", e);
		}
	}

	@Override
	public boolean hasBody() {
		return true;
	}

	@Override
	public void addBodyExpression(Expression e) {
		body.add(e);
	}

	@Override
	public int getSourceLine() {
		return sourceLine;
	}

	@Override
	public void setSourceLine(int line) {
		sourceLine = line;
	}

	@Override
	public Expression getParent() {
		return parent;
	}

}
